package com.xiaohudie.JDBC;

import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * @Author xiao hu die
 * @Date 2022/3/13 14:40
 * @Version 1.0
 */
public class DbUtils {
 private static Properties properties =new Properties();
 static {
     try {
         properties.load(new FileReader("src//dp.properties"));
         String driver = properties.getProperty("driver");
         try {
             Class.forName(driver);
         } catch (ClassNotFoundException e) {
             e.printStackTrace();
         }
     } catch (IOException e) {
         e.printStackTrace();
     }
 }
 private DbUtils(){

 }
 public static Connection getConnection() throws SQLException {

     String user = properties.getProperty("user");
     String url = properties.getProperty("url");
     String password = properties.getProperty("password");
     Connection connection = DriverManager.getConnection(url, user, password);
    return connection;
 }
 /**关闭资源*/
 public static void close(Connection connection, Statement statement, ResultSet resultSet){
     if (resultSet!=null){
         try {
             resultSet.close();
         } catch (SQLException e) {
             e.printStackTrace();
         }
     }
     if (statement!=null){
         try {
             statement.close();
         } catch (SQLException e) {
             e.printStackTrace();
         }
     }
     if (connection!=null){
         try {
             connection.close();
         } catch (SQLException e) {
             e.printStackTrace();
         }
     }
 }

}
